import time

from selenium.webdriver.common.by import By

from base.app_base_page import AppBasePage


class PageLogin(AppBasePage):
    def __init__(self, driver):
        super().__init__(driver)
        self.__me_btn = (By.XPATH, "//*[@text='我的']")
        self.__head_img = (By.ID, "com.tpshop.malls:id/head_img")
        self.__user_name = (By.XPATH, "//*[@text='请输入账号']")
        self.__pwd = (By.XPATH, "//*[@text='请输入密码']")
        self.__agree_btn = (By.ID, "com.tpshop.malls:id/agree_btn")
        self.__login_btn = (By.ID, "com.tpshop.malls:id/login_tv")
        self.__success_result = (By.ID, "com.tpshop.malls:id/nick_name_tv")
        self.__fail_result = (By.ID, "com.tpshop.malls:id/login_tv")
        self.__toast_result = (By.XPATH, '//android.widget.Toast')

    def page_login(self, user, pwd):
        self.app_base_click(self.__me_btn)
        self.app_base_click(self.__head_img)
        self.app_base_send_keys(self.__user_name, user)
        self.app_base_send_keys(self.__pwd, pwd)
        self.app_base_click(self.__agree_btn)
        self.app_base_click(self.__login_btn)
        time.sleep(2)

    def get_success_result(self):
        time.sleep(1)
        return self.app_fd_element(self.__success_result).text

    def get_fail_result(self):
        time.sleep(1)
        return self.app_fd_element(self.__fail_result).text

    def get_toast_result(self):
        time.sleep(1)
        return self.app_fd_element(self.__toast_result).text
